package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.SensitiveDto;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.wemedia.mapper.WmSensitiveMapper;
import com.heima.wemedia.service.WmSensitiveService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
@Slf4j
@Transactional
public class WmSensitiveServiceImpl extends ServiceImpl<WmSensitiveMapper, WmSensitive> implements WmSensitiveService {

    @Autowired
    private WmSensitiveMapper wmSensitiveMapper;
    /**
     * 删除敏感词
     * @param id
     * @return
     */
    @Override
    public ResponseResult deleteSensitive(Integer id) {
        wmSensitiveMapper.deleteById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 查询敏感词列表
     * @param dto
     * @return
     */
    @Override
    public ResponseResult findList(SensitiveDto dto) {
        //检查参数
        dto.checkParam();
        //分页查询
        IPage page = new Page(dto.getPage(),dto.getSize());
        //创建一个lambdaQueryWrapper
        LambdaQueryWrapper<WmSensitive> lambdaQueryWrapper = Wrappers.<WmSensitive>lambdaQuery();
        //根据敏感词名称模糊查询
        if (dto.getName() != null){
            lambdaQueryWrapper.like(WmSensitive::getSensitives,dto.getName());
        }
        //根据时间倒序查询
        lambdaQueryWrapper.orderByDesc(WmSensitive::getCreatedTime);
        page = page(page,lambdaQueryWrapper);
        PageResponseResult responseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());
        responseResult.setData(page.getRecords());
        return responseResult;
    }
    /**
     * 新增敏感词
     * @param wmSensitive
     * @return
     */
    @Override
    public ResponseResult saveSensitive(WmSensitive wmSensitive) {

        WmSensitive sensitive = wmSensitiveMapper.selectOne(Wrappers.<WmSensitive>lambdaQuery().eq(WmSensitive::getSensitives, wmSensitive.getSensitives()));
        //判断敏感词是否重复
        if (sensitive !=null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"该敏感词已存在");
        }
        wmSensitive.setCreatedTime(new Date());
        wmSensitiveMapper.insert(wmSensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
    /**
     * 修改敏感词
     * @param wmSensitive
     * @return
     */
    @Override
    public ResponseResult updateSensitive(WmSensitive wmSensitive) {
        WmSensitive sensitive = wmSensitiveMapper.selectOne(Wrappers.<WmSensitive>lambdaQuery().eq(WmSensitive::getSensitives, wmSensitive.getSensitives()));
        //判断敏感词是否重复
        if (sensitive !=null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"该敏感词已存在");
        }
        wmSensitiveMapper.updateById(wmSensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
